package com.generation.builder;

import com.generation.Bean.Constants;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Map;
import java.util.Properties;

/**
 * @author mockingjay
 * @date 2023/4/9 - 14:41
 * 创建基础配置类(一些工具包)
 */
public class BuildBase {

    private static Logger LOGGER= LoggerFactory.getLogger(BuildBase.class);

    public static void excute(){
        //生成date枚举
        build("DateTimePatternEnum",Constants.PATH_ENUM,Constants.TEMPLATE_DATETIMEPATTERNENUM);
        //生成工具类
        build("DateUtils",Constants.PATH_UTILS,Constants.TEMPLATE_DATEUTILS);
        build("PageUtils",Constants.PATH_UTILS,Constants.TEMPLATE_PAGEUTILS);
        build("Query",Constants.PATH_UTILS,Constants.TEMPLATE_QUERYUTILS);
        build("Constants",Constants.PATH_UTILS,Constants.TEMPLATE_CONTSANTS);
        build("R",Constants.PATH_UTILS,Constants.TEMPLATE_R);
    }

    private static void build(String filename ,String outputPath,String whereTem){
        File folder=new File(outputPath);
        if(!folder.exists()){
            folder.mkdir();
        }
        File javaFile=new File(outputPath,filename+".java");
        OutputStream os=null;
        OutputStreamWriter outw=null;
        BufferedWriter bw=null;

        InputStream in =null;
        InputStreamReader inr=null;
        BufferedReader br=null;
        try {
            os=new FileOutputStream(javaFile);
            outw=new OutputStreamWriter(os);
            bw=new BufferedWriter(outw);

            //配置velocity资源加载器
            Properties prop = new Properties();
            prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            Velocity.init(prop);
            //封装mapper模板数据
            Map<String,Object> mapperTemplateData=Constants.TEMPLATE_BASE_DATA;
            VelocityContext context = new VelocityContext(mapperTemplateData);
            String templatePath=whereTem;
            Template tpl = Velocity.getTemplate(templatePath, "UTF-8");
            tpl.merge(context, bw);
        }catch (Exception e){
            LOGGER.error("构建基础包失败:{}",e);
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inr!=null){
                try {
                    inr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw!=null){
                try {
                   bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outw!=null){
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
